package net.lainiao.morethread.Chapter5.demo5;

import java.util.concurrent.RecursiveTask;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2017/3/6.
 */
public class SearchNumberTask extends RecursiveTask<Integer> {
    private int numbers[];
    private int start, end;
    private int number;
    private TaskManager manager;
    private final static int NOT_FOUND = -1;

    public SearchNumberTask(int numbers[], int start, int end, int number, TaskManager manager) {
        this.number = number;
        this.numbers = numbers;
        this.start = start;
        this.end = end;
        this.manager = manager;
    }

    @Override
    protected Integer compute() {
        System.out.println("Task:" + start + ":" + end);
        int ret;
        if (end - start > 10) {
            ret = launchTasks();
        } else {
            ret = lookForNumber();
        }
        return null;
    }

    private int lookForNumber() {
        for (int i = start; i < end; i++) {
            if (numbers[i] == number) {
                System.out.println("Task: Number " + number + " found in position " + i);
                manager.setResult(i);
                manager.cancelTask(this);
                return i;
            }
        }
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return NOT_FOUND;
    }

    private int launchTasks() {
        int mid=(start+end)/2;
        SearchNumberTask task1=new SearchNumberTask(numbers,start,mid,number,manager);
        SearchNumberTask task2=new SearchNumberTask(numbers,mid,end,number,manager);
        manager.addTask(task1);
        manager.addTask(task2);
        task1.fork();
        task2.fork();
        int returnValue=task1.join();
        if(returnValue!=-1){
            return returnValue;
        }
        returnValue=task2.join();
        return returnValue;
    }

    public void writeCancelMessage(){
        System.out.println("Task Canceled task from "+start+" to "+end);
    }
}
